package com.github.edu.repair.manager.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.github.admin.edu.assembly.common.entity.JsonArray;
import com.github.admin.edu.assembly.common.entity.JsonEntity;
import com.github.admin.edu.assembly.common.util.JsonUtils;
import com.github.admin.edu.assembly.common.util.ObjectUtil;
import com.github.admin.edu.assembly.string.util.StringUtils;
import com.github.admin.edu.orm.serivce.impl.DataRestfulAbstractBaseService;
import com.github.edu.repair.manager.domain.WlflServerDomain;
import com.github.edu.repair.manager.entity.TBxRwfp;
import com.github.edu.repair.manager.entity.TBxWlfl;
import com.github.edu.repair.manager.entity.TBxWlk;
import com.github.edu.repair.manager.service.WlflServerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * user:wangj
 * date:2018/8/20
 * Time: 21:48
 */
@Slf4j
@Service
public class WlflServerServiceImpl extends DataRestfulAbstractBaseService<TBxWlfl,Integer> implements WlflServerService {

    @Autowired
    private WlflServerDomain domain;

    @Override
    public String getAllGetEntity(String title, Integer current, Integer rows, String order, String fileId) {
        TBxWlfl tSysUser=new TBxWlfl();
        if(StringUtils.isNotBlank(title)){
            tSysUser.setMc(title);
        }
        return super.getAllGetEntity(tSysUser,current,rows,order,fileId);
    }

    @Override
    public String getAllEntity(String title, String order, String fileId) {
        return getAllGetEntity(title,0,0,order,fileId);
    }

    @Transactional
    @Override
    public String saveOrUpdate(String jsonEntity) {
        if(StringUtils.isNotBlank(jsonEntity)){
            JsonEntity<TBxWlfl> tJsonEntity= JsonUtils.toCollection(jsonEntity, new TypeReference<JsonEntity<TBxWlfl>>() {
            });
            TBxWlfl t=tJsonEntity.getData();
            Object o= ObjectUtil.getValueByKey(t,"id");
            if(org.springframework.util.StringUtils.isEmpty(o)){
                Field f = null;
                try {
                    f = t.getClass().getDeclaredField("id");
                    f.setAccessible(true);
                    f.set(t,getId(t.getClass().getSimpleName()));
                } catch (NoSuchFieldException e) {
                    log.info("error!!"+e.getMessage());
                    return JsonUtils.toJson(new JsonEntity<TBxWlfl>("反序列化错误！"));
                } catch (IllegalAccessException e) {
                    log.info("error!!"+e.getMessage());
                    return JsonUtils.toJson(new JsonEntity<TBxWlfl>("反序列化错误！"));
                }
            }
            t=domain.save(t);
            tJsonEntity.setData(t);
            return JsonUtils.toJson(tJsonEntity);
        }
        return JsonUtils.toJson(new JsonEntity<TBxWlfl>("保存对象为空！"));
    }
    @Override
    public String getALLPostByPager(String jsonEntity) {
        if(StringUtils.isNotBlank(jsonEntity)){
            JsonEntity<TBxWlfl> tJsonEntity= JsonUtils.toCollection(jsonEntity, new TypeReference<JsonEntity<TBxWlfl>>() {
            });
            if(null!=tJsonEntity){
                return super.getALLPostByPager(tJsonEntity.getData(),tJsonEntity.getPager());
            }
        }
        return JsonUtils.toJson(new JsonEntity<TBxWlfl>("未找到参数！"));
    }
    @Transactional
    @Override
    public String delete(String ids) {
        return super.delete(ids,"Integer");
    }

    @Override
    public String getAllObject() {
        List<TBxWlfl> list=domain.getAllBySfqy();
        JsonArray<TBxWlfl> jsonArray=new JsonArray<>();
        jsonArray.setDataList(list);
        return JsonUtils.toJson(jsonArray);
    }
}
